package com.woniu.sharepark.leasesystem.common.service.impl;

import com.woniu.sharepark.common.JsonResult;
import com.woniu.sharepark.common.dao.RoleDAO;
import com.woniu.sharepark.common.dao.UserInfoDAO;
import com.woniu.sharepark.common.pojo.Login;
import com.woniu.sharepark.common.pojo.UserInfo;
import com.woniu.sharepark.leasesystem.common.dao.DrivingPermitDAO;
import com.woniu.sharepark.leasesystem.common.dto.IDCardDTO;
import com.woniu.sharepark.leasesystem.common.dto.UserInfoDTO;
import com.woniu.sharepark.leasesystem.common.pojo.DrivingPermit;
import com.woniu.sharepark.leasesystem.common.service.IDCardService;
import com.woniu.sharepark.leasesystem.common.util.VerifyUtil;
import com.woniu.sharepark.platformsystem.pojo.Role;
import com.woniu.sharepark.util.BaiduVerifyUtil;
import com.woniu.sharepark.util.GetLoginUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @ClassName: IDCardImpl
 * @Description: 此类为身份证识别接口的对应实现类
 * @Author: Zongjie.Li
 * @Date: 2019/6/3
 * @Version: 1.0
 **/

@Service
public class IDCardImpl implements IDCardService {

    @Autowired
    private UserInfoDTO userInfoDTO;

    @Autowired
    private UserInfo userInfo;

    @Autowired
    private UserInfoDAO userInfoDAO;

    @Resource
    private DrivingPermitDAO drivingPermitDAO;

    @Resource
    private RoleDAO roleDAO;
    @Override
    public JsonResult idCardVerify(IDCardDTO idCardDTO) throws IOException {
        /*if (idCardDTO.getName() == null || idCardDTO.getName().equals("") ||
                idCardDTO.getCard() == null || idCardDTO.getCard().equals("") ||
                idCardDTO.getFrontImage().getOriginalFilename() == null || idCardDTO.getFrontImage().getOriginalFilename().equals("") ||
                idCardDTO.getBackImage().getOriginalFilename() == null || idCardDTO.getBackImage().getOriginalFilename().equals("")) {
            return JsonResult.error("请输入完整信息");
        }
        userInfoDTO = BaiduVerifyUtil.idcardRecognition(idCardDTO,userInfoDTO);

        Integer rows = RealIdCard.realId(userInfoDTO.getName(),userInfoDTO.getCard());
        if (rows != 200){
            return JsonResult.error("实名认证失败");
        }

        if (userInfoDTO == null || userInfoDTO.getName() == null || userInfoDTO.getName().equals("") ||
                userInfoDTO.getSex() == null || userInfoDTO.getSex().equals("") ||
                userInfoDTO.getCard() == null || userInfoDTO.getCard().equals("") ||
                userInfoDTO.getInvalidateTime() == null){
            return JsonResult.error("无法识别图片信息，请重新上传");
        }
        if (userInfoDTO.getInvalidateTime().getTime() < System.currentTimeMillis()){
            return JsonResult.error("身份信息已过期，请使用在有效期内的身份证");
        }
        if (!userInfoDTO.getName().equals(idCardDTO.getName()) || !userInfoDTO.getCard().equals(idCardDTO.getCard())){
            return JsonResult.error("身份证信息不匹配");
        }



        //此时将身份证信息插入数据库
        {
            //假设获取当前用户的UserInfo
            userInfo = new UserInfo();
            userInfo.setuId(0);
        }
        userInfo.setuName(userInfoDTO.getName());
        userInfo.setuCard(userInfoDTO.getCard());
        userInfoDAO.updateByPrimaryKeySelective(userInfo);
        return JsonResult.ok("身份信息添加成功");*/
        return null;
    }

    @Override
    public JsonResult drivingIdCard(MultipartFile drivingImg) {
        if (drivingImg.getOriginalFilename()==null || drivingImg.getOriginalFilename().length() == 0){
            return  JsonResult.error("请上传机动车图片");
        }
        //vehicleLicense
        DrivingPermit drivingPermit = BaiduVerifyUtil.drivingPermitCard(drivingImg);
        JsonResult result = VerifyUtil.verifyDriving_permit(drivingPermit);
        if (result.getCode() == 500){
            return JsonResult.error(result.getMsg());
        }
        Session session = SecurityUtils.getSubject().getSession();
        Login login = (Login) session.getAttribute("currentUser");
        drivingPermit.setuId(login.getUserInfo().getuId());
        drivingPermitDAO.insertSelective(drivingPermit);
        return JsonResult.ok("行驶证信息上传成功");
    }

    @Override
    public JsonResult checkIdCardAndDriving(Integer uId) {

         UserInfo userInfo = userInfoDAO.selectByPrimaryKey(uId);

         if (userInfo.getuCard() == null || userInfo.getuCard().length() == 0 ){
             return JsonResult.error("请前往认证");
         }
         Role role  = roleDAO.selectByUserRid(GetLoginUtil.getLogin().getrId());
         if (role.getrName().equals("租客")){
            if (drivingPermitDAO.findPermitByUid(userInfo.getuId()) == null){
                return JsonResult.error("请上传行驶证信息");
            }
         }
         return JsonResult.ok("success");
    }
}
